package com.yanqu.road.server.logic.activity.condition.doublespring;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.ActivityValueParamArgs;
import com.yanqu.road.entity.activity.doublespring.enums.DoubleSpringZlTypeEnum;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.doublespring.DoubleSpringActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.patronsfund.PatronsFundModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.server.logic.activity.condition.daomu.DaoMuPvePersonRankArgs;

import java.math.BigInteger;

public class DoubleSpringZlCondition extends BaseNewRankActivityCondition {

    public DoubleSpringZlCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        ActivityValueParamArgs args = (ActivityValueParamArgs) object;
        //判断活动是否一致
        if (args.getActivityId() != this.getActivityConditionInfo().getActivityId()) {
            return;
        }
        //判断任务是否开启
        boolean flag = this.checkConditionOpen();
        if (!flag) {
            return;
        }
        //设置任务值
        BigInteger score = BigInteger.valueOf(args.getValue());
        if (score.compareTo(getValue()) > 0) {
            setValue(score);
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.DoubleSpringZl.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.DoubleSpringZl.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        //判断任务是否开启
        boolean flag = this.checkConditionOpen();
        if (!flag) {
           return false;
        }
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    /**
     * 检查是任务开启条件
     * @return
     */
    private boolean checkConditionOpen() {
        //判断是否否是付费档
        BigInteger typeBysfBig = BigInteger.valueOf(DoubleSpringZlTypeEnum.DOUBLE_SPRING_ZL_TYPE_BYSF.getType());
        BigInteger typeQbjjBig = BigInteger.valueOf(DoubleSpringZlTypeEnum.DOUBLE_SPRING_ZL_TYPE_QBJJ.getType());
        if(activityConditionInfo.getParamList().get(0).compareTo(typeBysfBig) == 0){
            //比翼双飞，判断是否购买礼包
            boolean buyFlag = player.getModule(DoubleSpringActivityModule.class).checkZlMallBuy(activityInfo.getActivityId(),
                    DoubleSpringZlTypeEnum.DOUBLE_SPRING_ZL_TYPE_BYSF.getType());
            if (!buyFlag) {
                return false;
            }
        }else if(activityConditionInfo.getParamList().get(0).compareTo(typeQbjjBig) == 0){
            //情比金坚，判断是否购买礼包
            boolean buyFlag = player.getModule(DoubleSpringActivityModule.class).checkZlMallBuy(activityInfo.getActivityId(),
                    DoubleSpringZlTypeEnum.DOUBLE_SPRING_ZL_TYPE_QBJJ.getType());
            if (!buyFlag) {
                return false;
            }
        }
        return true;
    }
}
